package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellVo;
import com.qf.vo.DayStatis;
import com.qf.vo.OrderAmtsVo;
import com.qf.vo.UserOrderCntVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/dashboard")
@CrossOrigin
public class DashboardController {

    @Autowired
    private DtsGoodsService dtsGoodsService;
    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;
    @Autowired
    private DtsOrderService dtsOrderService;
    @Autowired
    private DtsCategoryService dtsCategoryService;
    @Autowired
    private DtsAdminService dtsAdminService;


    private static final int STATIS_DAYS_RANG= 30;

    /**
     * {
     *     "errno":0,
     *     "data":{
     *         "goodsTotal":426,
     *         "userTotal":197,
     *         "productTotal":8939,
     *         "orderTotal":107
     *     },
     *     "errmsg":"成功"
     * }
     * @return
     */
    @GetMapping
    public Object dashboard(){
        Map data = new HashMap();
        Long goodsTotal = dtsGoodsService.selectCount();
        Long userTotal =  dtsUserService.selectCount();
        Long productTotal = dtsGoodsProductService.selectCount();
        Long orderTotal = dtsOrderService.selectCount();
        data.put("goodsTotal",goodsTotal);
        data.put("userTotal",userTotal);
        data.put("productTotal",productTotal);
        data.put("orderTotal",orderTotal);
        return ResponseUtil.ok(data);
    }

    /**
     *
     * - 首页数据可视化图表地址 : http://localhost:8083/admin/dashboard/chart
     *
     * - GET请求
     *
     * - 下行数据 :
     *
     * - categorySell : 商品分类  和对应分类的销售额
     * @return
     */
    @GetMapping("/chart")
    public Object chart(){
        /**
         *
         * categorySell : 商品分类  和对应分类的销售额
         * "categorySell":{
         *             "categoryNames":["数码家电","日用百货","鞋帽箱包"],
         *             "categorySellData":[
         *                 {"name":"数码家电","value":68.76},
         *                 {"name":"日用百货","value":48.76},
         *                 {"name":"鞋帽箱包","value":277.53}
         *             ]
         *         }
         */
        CategorySellVo categorySellVo = dtsCategoryService.findCategorySell();
        /**
         * "orderAmts":{
         *             "dayData":[
         *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
         *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
         *                 "2119-12-11","2119-12-13","2119-12-15","2119-12-16",
         *                 "2119-12-17","2119-12-19","2119-12-20","2119-12-23",
         *                 "2119-12-25","2119-12-27","2119-12-28","2119-12-29",
         *                 "2119-12-30"
         *             ],
         *             "orderAmtData":[
         *                 6.88,138.79,277.65,65.64,182.00,61.00,
         *                 289.01,375.76,29.50,247.50,145.50,
         *                 31.18,399.44,16.99,150.01,218.65,255.77,
         *                 126.59,275.25,97.74,14.63
         *             ],
         *             "orderCntData":[
         *                 1,2,4,1,3,1,2,3,1,1,16,3,9,2,1,2,2,2,2,2,2
         *             ]
         *         }
         *
         *  orderAmts : 日期和对应的订单总额, 订单笔数
         */
        List<DayStatis> dayStatisList =  dtsOrderService.findOrderAmtVo(STATIS_DAYS_RANG);
        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setOrderCntData(dayStatisList.stream().mapToInt(DayStatis::getCnts).toArray());
        orderAmtsVo.setDayData(dayStatisList.stream().map(DayStatis::getDayStr).toArray(value -> new String[dayStatisList.size()]));
        orderAmtsVo.setOrderAmtData(dayStatisList.stream().map(DayStatis::getAmts).toArray(value -> new BigDecimal[dayStatisList.size()]));

        /**
         * "userOrderCnt":{
         *             "dayData":[
         *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
         *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
         *                 "2119-12-09","2119-12-10","2119-12-11","2119-12-12",
         *                 "2119-12-13","2119-12-14","2119-12-15","2119-12-16",
         *                 "2119-12-17","2119-12-18","2119-12-19","2119-12-20",
         *                 "2119-12-21","2119-12-22","2119-12-23","2119-12-24",
         *                 "2119-12-25","2119-12-26","2119-12-27","2119-12-28",
         *                 "2119-12-29","2119-12-30"
         *             ],
         *             "userCnt":[
         *                 5,9,13,7,6,2,1,3,17,6,2,9,8,17,23,8,
         *                 4,8,8,14,3,2,1,1,2,1,2,7,3,5
         *             ],
         *             "orderCnt":[
         *                 1,2,4,1,3,1,2,3,0,0,1,0,1,0,16,3,9,
         *                 0,2,1,0,0,2,0,2,0,2,2,2,2
         *             ]
         *         }
         *
         *  userOrderCnt : 日期对应的用户数和订单数
         */
        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        List<DayStatis> dayStatisList1 = dtsUserService.findUserOrderCntVo(STATIS_DAYS_RANG);
        userOrderCntVo.setUserCnt(dayStatisList1.stream().mapToInt(DayStatis::getCnts).toArray());
        userOrderCntVo.setDayData(dayStatisList1.stream().map(DayStatis::getDayStr).toArray(value -> new String[dayStatisList1.size()]));
        userOrderCntVo = marageOrderCntAndUserCnt(dayStatisList,dayStatisList1);
        Map data = new HashMap();
        data.put("categorySell",categorySellVo);
        data.put("orderAmts",orderAmtsVo);
        data.put("userOrderCnt",userOrderCntVo);

        return ResponseUtil.ok(data);
    }
    private UserOrderCntVo marageOrderCntAndUserCnt(List<DayStatis> dayStatisList,List<DayStatis> dayStatisList1){
        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        Set<String> data  = new HashSet<>();
        for (DayStatis dayStatis : dayStatisList) {
            data.add(dayStatis.getDayStr());
        }
        for (DayStatis dayStatis : dayStatisList1) {
            data.add(dayStatis.getDayStr());
        }
        ArrayList<String> list = new ArrayList<>(data);
        Collections.sort(list);
        String[] array = list.toArray(new String[list.size()]);
        userOrderCntVo.setDayData(array);
        Map<String, Integer> orderMap = dayStatisList.stream().collect(Collectors.toMap(DayStatis::getDayStr, DayStatis::getCnts));
        int[] cnt1  = list.stream().mapToInt(dayDatum -> {
            Integer cnt = orderMap.get(dayDatum);
            if (null == cnt) {
                cnt = 0;
            }
            return cnt;
        }).toArray();
        Map<String, Integer> userMap = dayStatisList1.stream().collect(Collectors.toMap(DayStatis::getDayStr, DayStatis::getCnts));
        int[] cnt2 = list.stream().mapToInt(dayDatum -> {
            Integer cnt = userMap.get(dayDatum);
            if (null == cnt) {
                cnt = 0;
            }
            return cnt;
        }).toArray();
        userOrderCntVo.setOrderCnt(cnt1);
        userOrderCntVo.setUserCnt(cnt2);
        return userOrderCntVo;

}



}
